<template>
  <div>
    <el-dialog title="分享" :visible.sync="dialogData.isShow">
      <el-form :model="dialogData">
        <el-form-item label="链接">
          <el-input
            disabled
            style="width:70%"
            v-model="dialogData.url"
            autocomplete="off"
          ></el-input>
          <el-button
            style="height:40px;margin-left:10px"
            type="success"
            @click="copy(dialogData.url)"
            v-copy="dialogData.url"
          >
            一键复制
          </el-button>
        </el-form-item>
        <el-form-item label="二维码">
          <img :src="imgUrl" ref="qrCodeUrl" alt="推广二维码" />
          <el-button type="success" style="margin-left:50px" @click="down">
            <a :href="imgUrl" download="二维码" style="color:white">下载图片</a>
          </el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogData.isShow = false">取 消</el-button>
        <el-button type="success" @click="dialogData.isShow = false">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import QRCode from 'qrcode'
import { copy } from 'v-copy'
export default {
  directives: { copy },
  props: ['dialogData'],
  data() {
    return { imgUrl: '' }
  },
  mounted() {
    const { url } = this.dialogData
    this.getQRcode(url).then((img) => {
      this.imgUrl = img
    })
  },
  methods: {
    /* 生成二维码 */
    async getQRcode(url) {
      const cofing = {
        margin: 1,
        quality: 1,
        type: 'image/jpeg',
        errorCorrectionLevel: 'L',
        color: { dark: '#000', light: '#fff' }
      }
      try {
        const qrcode = await QRCode.toDataURL(url, cofing)
        return qrcode
      } catch (err) {
        console.error(err)
      }
    },
    /* 复制 */
    copy(val) {
      val ? this.$message.success('已复制!') : this.$message.success('无数据!')
    },
    /* 下载图片 */
    down() {}
  }
}
</script>

<style lang="less" scoped></style>
